package com.wenx.v3system.modular.platform.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wenx.v3dynamicdatasourcestarter.config.DynamicDataSourceConfig;
import com.wenx.v3system.modular.platform.domain.dto.PlatformMenuDto;
import com.wenx.v3system.modular.platform.domain.maps.PlatformMenuMap;
import com.wenx.v3system.modular.platform.domain.po.PlatformMenu;
import com.wenx.v3system.modular.platform.mapper.PlatformMenuMapper;
import com.wenx.v3system.modular.platform.service.PlatformMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 平台菜单服务实现类
 * 简化权限模型 - 基于菜单的权限控制
 * 使用v3-platform数据源
 * 
 * @author wenx
 */
@Slf4j
@Service
@DS(DynamicDataSourceConfig.PLATFORM_DATASOURCE)
public class PlatformMenuServiceImpl extends ServiceImpl<PlatformMenuMapper, PlatformMenu> implements PlatformMenuService {

    @Override
    public List<PlatformMenuDto> getAllMenus() {
        List<PlatformMenu> menus = this.list(
            new LambdaQueryWrapper<PlatformMenu>()
                .orderByAsc(PlatformMenu::getSort)
        );
        return menus.stream()
                .map(PlatformMenuMap.INSTANCE::toDto)
                .collect(Collectors.toList());
    }

    @Override
    public List<Map<String, Object>> getAllMenusAsMap() {
        List<PlatformMenuDto> menuDtos = getAllMenus();
        return menuDtos.stream()
                .map(this::convertToMenuMap)
                .collect(Collectors.toList());
    }

    @Override
    public Map<String, List<Map<String, Object>>> getMenuGroupsAsMap() {
        List<PlatformMenuDto> menuDtos = getAllMenus();
        return menuDtos.stream()
                .collect(Collectors.groupingBy(
                        menu -> menu.getCategory() != null ? menu.getCategory() : "default",
                        Collectors.mapping(this::convertToMenuMap, Collectors.toList())
                ));
    }

    @Override
    public List<String> getAllMenuPermissionCodes() {
        List<PlatformMenuDto> menuDtos = getAllMenus();
        return menuDtos.stream()
                .map(PlatformMenuDto::getCode)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }

    /**
     * 将PlatformMenuDto转换为Map格式
     * 用于前端组件数据展示
     */
    private Map<String, Object> convertToMenuMap(PlatformMenuDto menuDto) {
        Map<String, Object> menu = new HashMap<>();
        menu.put("key", menuDto.getCode());
        menu.put("label", menuDto.getName());
        menu.put("icon", menuDto.getIcon());
        menu.put("path", menuDto.getPath());
        menu.put("order", menuDto.getSort());
        menu.put("hidden", menuDto.getStatus() != null && menuDto.getStatus() == 0);
        
        // 处理权限要求
        if (menuDto.getRequiredPermissions() != null) {
            String[] permissions = menuDto.getRequiredPermissions().split(",");
            menu.put("requiredPermissions", Arrays.asList(permissions));
        }
        
        return menu;
    }

}